In [1]:
import pandas as pd
import netCDF4
from mpl_toolkits.basemap import interp
In [2]:
df = pd.read_csv('/usgs/data2/notebook/right_whale.csv',index_col='DATE',parse_dates=True)
In [3]:
df.head(10)
Out[3]:
In [6]:
# read digital terrain data for Gulf of Maine
bathy_url='http://geoport.whoi.edu/thredds/dodsC/bathy/gom03_v1_0'
nc = netCDF4.Dataset(bathy_url).variables
box = [-71.4,41,-70.2,42]
lon=nc['lon'][:]
lat=nc['lat'][:]
bi=(lon>=df['Longitude'].min())&(lon<=df['Longitude'].max())
bj=(lat>=df['Latitude'].min())&(lat<=df['Latitude'].max())
z=nc['topo'][bj,bi]
lon=lon[bi]
lat=lat[bj]
In [7]:
# interpolate terrain to find depth at Whale Sighting locations
df['Depth']=interp(z,lon,lat,df['Longitude'],df['Latitude'])
In [8]:
df.head()
Out[8]:
In [9]:
# plot up a histogram of depth where whales are found
hist(df['Depth']);
In [10]:
# create dataframe of shallow sightings
ishallow = where(df['Depth'] > -10.0)[0]
df_shallow = df.ix[ishallow]
df_shallow.head(10)
Out[10]:
In [9]:
figure(figsize=(12,12))
z = ma.masked_where(z>0,z)
subplot(111,aspect=(1.0/cos(mean(lat)*pi/180.0)))
pcolormesh(lon,lat,z,vmax=0)
plot(df_shallow.Longitude,df_shallow.Latitude,'ko');
plot(df_shallow.Longitude,df_shallow.Latitude,'w+');
axis([-70.6, -70.0, 41.75, 42.06])
grid()
In [10]:
# determine stats by year (or month, quarter, etc)
#df_shallow_stats = df_shallow.resample('6M', how='mean') # monthly means
df_shallow_stats = df_shallow.resample('A-AUG', how='sum') # annual sum, ending in august
In [11]:
df_shallow_stats['SPECIES TOTAL']
Out[11]:
In [12]:
# any significant trend in time?
df_shallow_stats['SPECIES TOTAL'].plot(kind='bar',figsize=(12,5))
Out[12]:
In [13]:
# average number of sightings for each month
df_monthly_sum = df_shallow.groupby(lambda x: x.month).sum()
In [14]:
df_monthly_sum['SPECIES TOTAL']
Out[14]:
In [15]:
# group sightings by Month
grouped=df_shallow.groupby(lambda x: x.month)
In [16]:
# plot 6 frame panel of sightings in each month
iframe=0
figure(figsize=(20,10))
for month,group in grouped:
iframe = iframe + 1
subplot(2,3,iframe,aspect=(1.0/cos(mean(lat)*pi/180.0)))
pcolormesh(lon,lat,z,vmax=0)
plot(group.Longitude,group.Latitude,'ko')
plot(group.Longitude,group.Latitude,'w+')
axis('tight')
#axis('off')
grid('on')
title('Month = %d' % month)
In [17]:
for month,group in grouped:
group.to_csv('/usgs/data2/notebook/%s.csv' % month)
In [18]:
# plot each month separately
iframe=0
for month,group in grouped:
iframe = iframe + 1
#subplot(2,3,iframe,aspect=(1.0/cos(mean(lat)*pi/180.0)))
figure(iframe,figsize=(12,12))
subplot(111,aspect=(1.0/cos(mean(lat)*pi/180.0)))
pcolormesh(lon,lat,z,vmax=0)
plot(group.Longitude,group.Latitude,'ko')
plot(group.Longitude,group.Latitude,'w+')
axis([-70.6, -70.0, 41.75, 42.06])
#axis('tight')
#axis('off')
grid('on')
title('Month = %d' % month)
In [19]:
df_shallow_stats['SPECIES TOTAL'].sum()
Out[19]:
In [20]:
df_monthly_sum['SPECIES TOTAL'].sum()
Out[20]:
In [11]:
In [12]:
df_shallow.index
Out[12]:
In [13]:
type(df_shallow.index)
Out[13]:
In [ ]: